'''
Created on 2011-06-28

@author: Mikael
'''

from PyQt4 import Qt
import os.path


class GenericNode(object):
    '''
    Represent a generic node in a hierarchy
    '''

    def __init__(self, textData=[], isPath=1):
        '''
        Constructor
        textData: string array for each column of the node
        rowIdx: the row idx of the node
        '''
        self.mChildNodeList = []
        self.mParent = None
        self.mTextData = textData
        self.mVariant = []
        self.mIcon = None
        for strVal in textData:
            if isPath:
                self.mVariant.append(os.path.basename(strVal))
            else:
                self.mVariant.append(strVal)
            
  
        
            
    def getIcon(self):
        return self.mIcon
        
    def getText(self, column=0):
        '''
        Get text for a given column index
        column: the column index
        '''
        return self.mTextData[column]
    
    def getData(self, column=0):
        '''
        Get the data of the column as a QVariant
        '''
        if column > len(self.mVariant):
            return ""
        
        return self.mVariant[column]
    
    def getColumnCount(self):
        '''
        Get the node column count
        '''
        return len(self.mVariant)
    
    def getNodeCount(self):
        count = 1
        for subNode in self.mChildNodeList:
            count += subNode.getNodeCount()
        
        return count
            
        
    def addChildNode(self, Node):
        '''
        Add a child node to the node
        '''
        self.mChildNodeList.append(Node)
        Node.mParent = self
        
    def removeChildNode(self, Node):
        '''
        Remove a child node to the node
        '''
        self.mChildNodeList.remove(Node)
        Node.mParent = None
        
    def removeChildNodebyIdx(self, idx):
        '''
        Remove a child by it's index
        '''
        if idx < len(self.mChildNodeList) and idx >= 0:
            self.mChildNodeList.pop(idx)
            return 1
        return 0
        
        
    def insertChildenNodeIdx(self, GenericNode, idx):
        '''
        Insert a child to a given index
        '''
        self.mChildNodeList.insert(idx, GenericNode)
        GenericNode.mParent = self
        
    def getChildNodeAt(self, idx):
        '''
        Get a child node from a given idx
        '''
        if idx >= len(self.mChildNodeList):
            return None
        
        return self.mChildNodeList[idx]
    
    def getIndexOf(self, GenericNode):
        return self.mChildNodeList.index(GenericNode)
    
    def getChildIndexInParentNode(self):
        if self.mParent == None:
            return 0
        
        return self.mParent.getIndexOf(self)
    
    def getChildCount(self):
        return len(self.mChildNodeList)
    
    def setColumnData(self, columnIdx, QVariant):
        if columnIdx < 0 or columnIdx > len(self.mVariant):
            return 0

        self.mVariant[columnIdx] = QVariant
        self.mTextData[columnIdx] = str(QVariant)
        
        return 1
    
    def printHiearchy(self, indent=""):
        
        print(indent + self.mTextData[0])
        
        for subNode in self.mChildNodeList:
            subNode.printHiearchy(indent + "-")
